package com.financing.wap.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.wap.dao.IAccountDao;
import com.financing.wap.dao.IActivitieGiftDao;
import com.financing.wap.dao.IFpOrderRebateDao;
import com.financing.wap.dto.AccountCorpDTO;
import com.financing.wap.dto.AccountDetailDTO;
import com.financing.wap.dto.ActivitesDTO;
import com.financing.wap.dto.ActivitieGiftDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IAccountService;
import com.financing.wap.util.Constants;
import com.financing.wap.util.DateUtil;
import com.financing.wap.util.PropertiesUtils;
import com.financing.wap.vo.AccountCorpVo;
import com.financing.wap.vo.AccountDetailVo;
import com.financing.wap.vo.AmountVO;



@Service("accountService")
public class AccountServiceImpl implements IAccountService{

	@Autowired
	private IAccountDao accountDao;

	@Autowired
	private IActivitieGiftDao activitieGiftDaoImpl;


	@Autowired
	private IFpOrderRebateDao fpOrderRebateDao;


	Logger logger = Logger.getLogger(this.getClass());
	@Override
	public AccountCorpDTO getAccountCorpInfo(AccountCorpVo vo) {
		return accountDao.getAccountCorpInfo(vo);
	}

	@Override
	public List<AccountCorpDTO> getAccountDetailCorpList(
			AccountCorpVo vo) {
		return accountDao.getAccountDetailCorpList(vo);
	}

	@Override
	public int getAccountDetailCorpCount(AccountCorpVo vo) {
		return accountDao.getAccountDetailCorpCount(vo);
	}

	//今日平台产生总佣金，累计收入， 近一个月收入， 帐号余额
	public BigDecimal todayAllRebates(){
		//step1 计算平台今日总佣金
		Map<String, Object> paramMap = new HashMap<String, Object>(3);
		paramMap.put("startDate", DateUtil.getDateStarting(new Date()));
		BigDecimal todayAllRebates = fpOrderRebateDao.todayAllRebatesByDate(paramMap);  //今天所有返利
		logger.info("today total rebates: " + todayAllRebates);

		return todayAllRebates;
	}

	@Override
	public AccountCorpDTO getFaAccountByUserId(String id) {

		return accountDao.getFaAccountByUserId(id);
	}

	@Override
	public Integer updateAccountBalanceActivity(AccountCorpDTO dto) {
		return accountDao.updateAccountBalanceActivity(dto);
	}

	
	/** 
	 * 
	 ******************************************
	 * wap_jee-1.12
	 *  确认返佣（理财师已经收到返佣的钱）
	 *  lisu
	 ****************************************  
	 */
	@Override
	public Integer changeAccountByRebateConfirm(String userId,BigDecimal amount)throws Exception{
		//账户总表变更完成  余额扣除，冻结扣除
		AccountCorpDTO accountCorpDTO = accountDao.getFaAccountByUserId(userId);
		accountCorpDTO.setAccountBalanceActivity(accountCorpDTO.getAccountBalanceActivity());
		accountCorpDTO.setAccountBalanceSleep(accountCorpDTO.getAccountBalanceSleep().subtract(amount));
		accountCorpDTO.setAccountBalance(accountCorpDTO.getAccountBalance().subtract(amount));
		accountDao.updateAccountBalanceActivity(accountCorpDTO);
		//明细
		AccountDetailDTO dto = new AccountDetailDTO();
		dto.setUserId(userId);
		dto.setAccountId(accountCorpDTO.getId());
		dto.setAmount(amount.negate());
		dto.setAccountType(Constants.P_ACCOUNT_DETAIL_TYPE_OUTPUT);
		dto.setOperateType(Constants.P_ACCOUNT_OPERATE_REBATE_CONFIRM);
		dto.setOperateStatus(Constants.P_ACCOUNT_OPERATE_STATUS_SUCCESS);
		dto.setUpdateBy(userId.toString());
		dto.setCreateBy(userId.toString());
		accountDao.addUserAccountDetail(dto);
		
		return accountCorpDTO.getId();
	}
	
	
	@Override
	public  List<Map<String,Object>>  getCorpUserAmount(AmountVO queryVo) {
		return accountDao.getCorpUserAmount(queryVo);
	}



	@Override
	public Integer getCorpUserAmountCount(AmountVO queryVo) {

		return accountDao.getCorpUserAmountCount(queryVo);
	}
	@Override
	public  Map<String,Object>  getCorpAllAmount(String corpId) {

		return accountDao.getCorpAllAmount(corpId);
	}

	@Override
	public  List<Map<String,Object>>  getCorpDeptAmount(String corpId) {

		return accountDao.getCorpDeptAmount(corpId);
	}

	@Override
	public Integer getUserAccountDetailCount(AccountDetailVo queryVo) {

		return accountDao.getUserAccountDetailCount(queryVo);
	}

	@Override
	public List<AccountDetailDTO> getUserAccountDetailList(
			AccountDetailVo queryVo) {

		return accountDao.getUserAccountDetailList(queryVo);
	}

	@Override
	public Integer addUserAccountDetailByRebate(AccountDetailDTO dto) {
		dto.setAccountType(Constants.P_ACCOUNT_DETAIL_TYPE_INPUT);
		dto.setOperateType(Constants.P_ACCOUNT_OPERATE_REBATE);
		dto.setOperateStatus(Constants.P_ACCOUNT_OPERATE_STATUS_SUCCESS);
		return accountDao.addUserAccountDetail(dto);
	}
	@Override
	public Integer addUserAccountDetailByActivity(AccountDetailDTO dto) {
		AccountCorpDTO accountDto =  new AccountCorpDTO();
		BigDecimal amount = dto.getAmount();
		accountDto.setId(dto.getAccountId());
		accountDto.setAccountBalance(amount);
		accountDto.setAllAccount(amount);
		accountDto.setAccountBalanceSleep(amount);
		accountDto.setAccountBalanceActivity(new BigDecimal(0));
		accountDto.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
		accountDao.updateAccountBalanceActivity(accountDto);

		dto.setAccountType(Constants.P_ACCOUNT_DETAIL_TYPE_INPUT);
		dto.setOperateType(Constants.P_ACCOUNT_OPERATE_ACTIVITY);
		dto.setOperateStatus(Constants.P_ACCOUNT_OPERATE_STATUS_SUCCESS);
		dto.setCreateBy(Constants.PLATFORM_ADMIN_NAME);
		dto.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
		return accountDao.addUserAccountDetail(dto);
	}

	@Override
	public Integer addUserAccountDetailByActivityGift(AccountDetailDTO dto,
			String sourceCodeInt) throws Exception {
		//更新注册人用户信息
		AccountCorpDTO accountDto =  new AccountCorpDTO();
		BigDecimal amount = dto.getAmount();
		accountDto.setId(dto.getAccountId());
		accountDto.setAccountBalance(amount);
		accountDto.setAllAccount(amount);
		accountDto.setAccountBalanceSleep(amount);
		accountDto.setAccountBalanceActivity(new BigDecimal(0));
		accountDto.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
		accountDao.updateAccountBalanceActivity(accountDto);


		//添加注册人活动礼品信息
		ActivitieGiftDTO activitieGiftDTO = new ActivitieGiftDTO();
		activitieGiftDTO.setaType("01");
		activitieGiftDTO.setaStatus("01");
		activitieGiftDTO.setaSource("01");
		activitieGiftDTO.setaAmount(new BigDecimal(PropertiesUtils.getValue("activety_register_money")));
		activitieGiftDTO.setFaUserId(dto.getUserId());
		activitieGiftDTO.setCreateBy(Constants.PLATFORM_ADMIN_NAME);
		activitieGiftDTO.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
		activitieGiftDTO.setActiviesCode(Constants.ACTIVITIES_CODE_ZCSHB);
		activitieGiftDaoImpl.insertActivitieGift(activitieGiftDTO);

		//添加注册人账户明细
		dto.setAccountType(Constants.P_ACCOUNT_DETAIL_TYPE_INPUT);
		dto.setOperateType(Constants.P_ACCOUNT_OPERATE_ACTIVITY);
		dto.setOperateStatus(Constants.P_ACCOUNT_OPERATE_STATUS_SUCCESS);
		dto.setCreateBy(Constants.PLATFORM_ADMIN_NAME);
		dto.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
		dto.setActivitieGiftId(activitieGiftDTO.getId());
		accountDao.addUserAccountDetail(dto);

		//添加分添加分享人信息
		if(StringUtils.isNotBlank(sourceCodeInt)){
			//---更新分享人用户信息
			//查询分享人信息 
			AccountCorpDTO accountBean = accountDao.getFaAccountByUserId(sourceCodeInt);
			if(accountBean == null){
				return -1;
			}
			AccountCorpDTO accountDtoShare =  new AccountCorpDTO();
			BigDecimal amountShare = dto.getAmount();
			accountDtoShare.setId(accountBean.getId());
			accountDtoShare.setAccountBalance(accountBean.getAccountBalance().add(amountShare));
			accountDtoShare.setAllAccount(accountBean.getAllAccount().add(amountShare));
			accountDtoShare.setAccountBalanceSleep(accountBean.getAccountBalanceSleep().add(amountShare));
			accountDtoShare.setAccountBalanceActivity(accountBean.getAccountBalanceActivity());
			accountDtoShare.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
			accountDao.updateAccountBalanceActivity(accountDtoShare);

			//添加分享人活动礼品信息
			ActivitieGiftDTO activitieGiftDTOShare = new ActivitieGiftDTO();
			activitieGiftDTOShare.setaType("01");
			activitieGiftDTOShare.setaStatus("01");
			activitieGiftDTOShare.setaSource("02");
			activitieGiftDTOShare.setaAmount(new BigDecimal(PropertiesUtils.getValue("activety_register_money")));
			activitieGiftDTOShare.setFaUserId(sourceCodeInt);
			activitieGiftDTOShare.setCreateBy(Constants.PLATFORM_ADMIN_NAME);
			activitieGiftDTOShare.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
			activitieGiftDTOShare.setActiviesCode(Constants.ACTIVITIES_CODE_ZCSHB);
			activitieGiftDTOShare.setFaShareUserId(dto.getUserId());
			activitieGiftDaoImpl.insertActivitieGift(activitieGiftDTOShare);

			AccountDetailDTO  accountDetailShare = new AccountDetailDTO();
			//添加分享人账户信息
			accountDetailShare.setAccountId(accountBean.getId());
			accountDetailShare.setAmount(new BigDecimal(PropertiesUtils.getValue("activety_register_money")));
			accountDetailShare.setUserId(accountBean.getUserId());
			accountDetailShare.setRemark(PropertiesUtils.getValue("activety_title"));
			accountDetailShare.setAccountType(Constants.P_ACCOUNT_DETAIL_TYPE_INPUT);
			accountDetailShare.setOperateType(Constants.P_ACCOUNT_OPERATE_ACTIVITY);
			accountDetailShare.setOperateStatus(Constants.P_ACCOUNT_OPERATE_STATUS_SUCCESS);
			accountDetailShare.setCreateBy(Constants.PLATFORM_ADMIN_NAME);
			accountDetailShare.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
			accountDetailShare.setActivitieGiftId(activitieGiftDTOShare.getId());
			return accountDao.addUserAccountDetail(accountDetailShare);
		}

		return 0;
	}

	@Override
	public Integer shareGiftActivites(BigDecimal amountShare,String userId,String sourceCodeInt) throws Exception {
		//添加分添加分享人信息
		if(StringUtils.isNotBlank(sourceCodeInt)){
			//---更新分享人用户信息
			//查询分享人信息 
			AccountCorpDTO accountBean = accountDao.getFaAccountByUserId(sourceCodeInt);
			if(accountBean == null){
				return -1;
			}
			AccountCorpDTO accountDtoShare =  new AccountCorpDTO();
			accountDtoShare.setId(accountBean.getId());
			accountDtoShare.setAccountBalance(accountBean.getAccountBalance().add(amountShare));
			accountDtoShare.setAllAccount(accountBean.getAllAccount().add(amountShare));
			accountDtoShare.setAccountBalanceSleep(accountBean.getAccountBalanceSleep().add(amountShare));
			accountDtoShare.setAccountBalanceActivity(accountBean.getAccountBalanceActivity());
			accountDtoShare.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
			accountDao.updateAccountBalanceActivity(accountDtoShare);

			//添加分享人活动礼品信息
			ActivitieGiftDTO activitieGiftDTOShare = new ActivitieGiftDTO();
			activitieGiftDTOShare.setaType("01");
			activitieGiftDTOShare.setaStatus("01");
			activitieGiftDTOShare.setaSource("02");
			activitieGiftDTOShare.setaAmount(amountShare);
			activitieGiftDTOShare.setFaUserId(sourceCodeInt);
			activitieGiftDTOShare.setCreateBy(Constants.PLATFORM_ADMIN_NAME);
			activitieGiftDTOShare.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
			activitieGiftDTOShare.setActiviesCode(Constants.ACTIVITIES_CODE_FXSHB);
			activitieGiftDTOShare.setFaShareUserId(userId);
			activitieGiftDaoImpl.insertActivitieGift(activitieGiftDTOShare);

			AccountDetailDTO  accountDetailShare = new AccountDetailDTO();
			//添加分享人账户明细信息
			accountDetailShare.setAccountId(accountBean.getId());
			accountDetailShare.setAmount(amountShare);
			accountDetailShare.setUserId(accountBean.getUserId());
			accountDetailShare.setRemark(PropertiesUtils.getValue("activety_title"));
			accountDetailShare.setAccountType(Constants.P_ACCOUNT_DETAIL_TYPE_INPUT);
			accountDetailShare.setOperateType(Constants.P_ACCOUNT_OPERATE_ACTIVITY);
			accountDetailShare.setOperateStatus(Constants.P_ACCOUNT_OPERATE_STATUS_SUCCESS);
			accountDetailShare.setCreateBy(Constants.PLATFORM_ADMIN_NAME);
			accountDetailShare.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
			accountDetailShare.setActivitieGiftId(activitieGiftDTOShare.getId());
			return accountDao.addUserAccountDetail(accountDetailShare);
		}

		return 0;
	}

	@Override
	public Integer operatingActivities(ActivitesDTO bean,UserDTO user) throws Exception {
		
		logger.info("in AccountServiceImpl operatingActivities begin! 账户操作 开始");
		//当前活动金额
		BigDecimal amount = Constants.ACTIVITIES_MONEY;
		logger.info("in AccountServiceImpl ActiviesCode code:"+bean.getActiviesCode());
		logger.info("in AccountServiceImpl Activiesamount amount:"+amount);
		
		AccountDetailDTO dto = new AccountDetailDTO();
		dto.setAccountId(user.getAccountId());
		dto.setAmount(amount);
		dto.setUserId(user.getId());
		dto.setRemark(bean.getActivitesTitle());
		
		//更新注册人用户信息
		AccountCorpDTO accountDto =  new AccountCorpDTO();
		
		accountDto.setId(dto.getAccountId());
		accountDto.setAccountBalance(amount);
		accountDto.setAllAccount(amount);
		accountDto.setAccountBalanceSleep(amount);
		accountDto.setAccountBalanceActivity(new BigDecimal(0));
		accountDto.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
		int i = accountDao.updateAccountBalanceActivity(accountDto);
		logger.info("in AccountServiceImpl updateAccountBalanceActivity SUCCESS 更新注册人用户信息:"+i);

		//添加注册人活动礼品信息
		ActivitieGiftDTO activitieGiftDTO = new ActivitieGiftDTO();
		activitieGiftDTO.setaType("01");
		activitieGiftDTO.setaStatus("01");
		activitieGiftDTO.setaSource("01");
		activitieGiftDTO.setaAmount(amount);
		activitieGiftDTO.setFaUserId(dto.getUserId());
		activitieGiftDTO.setCreateBy(Constants.PLATFORM_ADMIN_NAME);
		activitieGiftDTO.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
		activitieGiftDTO.setActiviesCode(bean.getActiviesCode());
		int y = activitieGiftDaoImpl.insertActivitieGift(activitieGiftDTO);
		logger.info("in AccountServiceImpl insertActivitieGift SUCCESS 添加注册人活动礼品信息:"+y);

		//添加注册人账户明细
		dto.setAccountType(Constants.P_ACCOUNT_DETAIL_TYPE_INPUT);
		dto.setOperateType(Constants.P_ACCOUNT_OPERATE_ACTIVITY);
		dto.setOperateStatus(Constants.P_ACCOUNT_OPERATE_STATUS_SUCCESS);
		dto.setCreateBy(Constants.PLATFORM_ADMIN_NAME);
		dto.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
		dto.setActivitieGiftId(activitieGiftDTO.getId());
		int addCount = accountDao.addUserAccountDetail(dto);
		logger.info("in AccountServiceImpl addUserAccountDetail SUCCESS 添加注册人账户明细:"+addCount);
		
		logger.info("in AccountServiceImpl operatingActivities end! 账户操作结束");
		return addCount;
	}

	@Override
	public Integer addActivitesGifAccount(ActivitesDTO bean,String userId,String sourceCode,BigDecimal amount,String aType,String aSource,String aStatus)
			throws Exception {
		logger.info("in AccountServiceImpl addActivitesGifAccount begin! 账户操作 开始");
		//活动信息
		logger.info("in AccountServiceImpl ActiviesCode code:"+bean.getActiviesCode());
		logger.info("in AccountServiceImpl ActiviesCode amount:"+amount);
		logger.info("in AccountServiceImpl ActiviesCode userId:"+userId);
		logger.info("in AccountServiceImpl ActiviesCode sourceCode:"+sourceCode);
		logger.info("in AccountServiceImpl ActiviesCode aType:"+aType);
		logger.info("in AccountServiceImpl ActiviesCode aSource:"+aSource);
		
		
		//判断userid 是否存在
		if(StringUtils.isBlank(userId)){
			return -1;
		}
		
		//查询用户账户信息 
		AccountCorpDTO accountBean = accountDao.getFaAccountByUserId(userId);
		if(accountBean == null){
			return -1;
		}
		
		//更新userid用户账户信息
		AccountCorpDTO accountDto =  new AccountCorpDTO();
		accountDto.setId(accountBean.getId());
		accountDto.setAccountBalance(accountBean.getAccountBalance().add(amount));
		accountDto.setAllAccount(accountBean.getAllAccount().add(amount));
		accountDto.setAccountBalanceSleep(accountBean.getAccountBalanceSleep().add(amount));
		accountDto.setAccountBalanceActivity(accountBean.getAccountBalanceActivity());
		accountDto.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
		int i = accountDao.updateAccountBalanceActivity(accountDto);
		logger.info("in AccountServiceImpl updateAccountBalanceActivity SUCCESS 更新userid用户账户信息:"+i);
		
		//添加userid用户活动礼品信息
		ActivitieGiftDTO activitieGiftDTO = new ActivitieGiftDTO();
		activitieGiftDTO.setaType(aType);
		activitieGiftDTO.setaStatus(aStatus);
		activitieGiftDTO.setaSource(aSource);
		activitieGiftDTO.setaAmount(amount);
		activitieGiftDTO.setFaUserId(userId);
		activitieGiftDTO.setCreateBy(Constants.PLATFORM_ADMIN_NAME);
		activitieGiftDTO.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
		activitieGiftDTO.setActiviesCode(bean.getActiviesCode());
		activitieGiftDTO.setFaShareUserId(sourceCode);
		int y =activitieGiftDaoImpl.insertActivitieGift(activitieGiftDTO);
		logger.info("in AccountServiceImpl insertActivitieGift SUCCESS 添加userid用户活动礼品信息:"+y);

		//添加userid用户账户明细信息
		AccountDetailDTO  accountDetail = new AccountDetailDTO();
		accountDetail.setAccountId(accountBean.getId());
		accountDetail.setAmount(amount);
		accountDetail.setUserId(accountBean.getUserId());
		accountDetail.setRemark(bean.getActivitesTitle());
		accountDetail.setAccountType(Constants.P_ACCOUNT_DETAIL_TYPE_INPUT);
		accountDetail.setOperateType(Constants.P_ACCOUNT_OPERATE_ACTIVITY);
		accountDetail.setOperateStatus(Constants.P_ACCOUNT_OPERATE_STATUS_SUCCESS);
		accountDetail.setCreateBy(Constants.PLATFORM_ADMIN_NAME);
		accountDetail.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
		accountDetail.setActivitieGiftId(activitieGiftDTO.getId());
		int x =accountDao.addUserAccountDetail(accountDetail);
		logger.info("in AccountServiceImpl addUserAccountDetail SUCCESS 添加userid用户账户明细信息:"+x);
		logger.info("in AccountServiceImpl addActivitesGifAccount end! 账户操作结束");
		return x;
	}

	@Override
	public Integer addActivitesGif(ActivitesDTO bean, String userId,
			String sourceCode, BigDecimal amount, String aType, String aSource,String aStatus)
			throws Exception {
		logger.info("in AccountServiceImpl addActivitesGif begin! 账户操作 开始");
		//活动信息
		logger.info("in AccountServiceImpl ActiviesCode code:"+bean.getActiviesCode());
		logger.info("in AccountServiceImpl ActiviesCode amount:"+amount);
		logger.info("in AccountServiceImpl ActiviesCode userId:"+userId);
		logger.info("in AccountServiceImpl ActiviesCode sourceCode:"+sourceCode);
		logger.info("in AccountServiceImpl ActiviesCode aType:"+aType);
		logger.info("in AccountServiceImpl ActiviesCode aSource:"+aSource);
		//判断userid 是否存在
		if(StringUtils.isBlank(userId)){
			return -1;
		}
		//添加userid用户活动礼品信息
		ActivitieGiftDTO activitieGiftDTO = new ActivitieGiftDTO();
		activitieGiftDTO.setaType(aType);
		activitieGiftDTO.setaStatus(aStatus);
		activitieGiftDTO.setaSource(aSource);
		activitieGiftDTO.setaAmount(amount);
		activitieGiftDTO.setFaUserId(userId);
		activitieGiftDTO.setCreateBy(Constants.PLATFORM_ADMIN_NAME);
		activitieGiftDTO.setUpdateBy(Constants.PLATFORM_ADMIN_NAME);
		activitieGiftDTO.setActiviesCode(bean.getActiviesCode());
		activitieGiftDTO.setFaShareUserId(sourceCode);
		int y = activitieGiftDaoImpl.insertActivitieGift(activitieGiftDTO);
		logger.info("in AccountServiceImpl insertActivitieGift SUCCESS 添加userid用户活动礼品信息:"+y);
		logger.info("in AccountServiceImpl addActivitesGif end! 账户操作结束");
		return y;
	}
	
}
